<?php
/**
 * Created by PhpStorm.
 * User: Zwc
 * Date: 2025/11/12
 * Time: 17:13
 */

namespace app\admin\service;

use app\admin\model\ConstellationModel;
use think\facade\Log;

class ConstellationService
{
    /**
     * 获取数据
     * @param $params
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getData($params): \think\Paginator
    {
        $condition = [
            ['del_flag', '=', 0],
        ];
        if (!empty($params['langId'])) {
            $condition[] = ['lang_id', '=', $params['langId']];
        }
        $model = new ConstellationModel();
        $data = $model->where($condition)->order('id desc')->paginate(20);

        foreach ($data as &$val) {
            if (!empty($val['cover_src'])) {
                $val['coverSrc'] = get_upload_path($val['cover_src']);
            }else {
                $val['coverSrc'] = '';
            }
        }
        return $data;
    }

    public function add($params)
    {
        $condition = [
            ['del_flag', '=', 0],
            ['lang_id', '=', $params['lang_id']],
            ['name', '=', $params['name']],
        ];
        $model = new ConstellationModel();
        $data = $model->where($condition)->find();
        if ($data) {
            return ['code' => -1, 'msg' => '星座已存在'];
        }
        $insertData = [
            'name' => $params['name'],
            'name_cn' => $params['name_cn'],
            'lang_id' => $params['lang_id'],
//            'cover_src' => $params['icon_src'],
        ];

        try {
            $model->insert($insertData);
            return ['code' => 0, 'msg' => '操作成功'];
        }catch (\Exception $e) {
            Log::error($e->getMessage());
            Log::error($e->getTraceAsString());
            return ['code' => 0, 'msg' => '操作失败，请联系管理员'];
        }
    }

    /**
     * 获取详情
     * @param $id
     * @return VipExplainModel|array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail($id)
    {
        $model = new ConstellationModel();
        return $model->find($id);
    }

    public function modify($params)
    {
        $condition = [
            ['del_flag', '=', 0],
            ['lang_id', '=', $params['lang_id']],
            ['name', '=', $params['name']],
            ['id', '<>', $params['id']],
        ];
        $model = new ConstellationModel();
        $data = $model->where($condition)->find();
        if ($data) {
            return ['code' => -1, 'msg' => '星座已存在'];
        }
        $upData = [
            'name' => $params['name'],
            'lang_id' => $params['lang_id'],
//            'cover_src' => $params['cover_src'],
            'name_cn' => $params['name_cn'],
        ];

        try {
            $model->where(['id' => $params['id']])->data($upData)->save();
            return ['code' => 0, 'msg' => '操作成功'];
        }catch (\Exception $e) {
            Log::error($e->getMessage());
            Log::error($e->getTraceAsString());
            return ['code' => 0, 'msg' => '操作失败，请联系管理员'];
        }
    }

    public function del($id)
    {
        $model = new ConstellationModel();
        $data = $model->where(['id' => $id])->find();
        try {
            $data['del_flag'] = 1;
            $data->save();
            return ['code' => 0, 'msg' => '操作成功'];
        }catch (\Exception $e) {
            Log::error($e->getMessage());
            Log::error($e->getTraceAsString());
            return ['code' => -1, 'msg' => '操作失败'];
        }
    }


}